﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StrategyService.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System;
    using System.Collections.Generic;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.LiveObjects;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The strategy service.
    /// </summary>
    [Credits("Feed Service", "Handles all strategy operations", "David Denis", "Copyright (C) David Denis <mailto:david.denis@systemathics.com>")]
    [ImplementsInterface("Org.OpenTrader.Framework.Forge.Interfaces.IStrategyService")]
    public sealed class StrategyService : Service, IStrategyService
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StrategyService"/> class.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <param name="container">
        /// The container.
        /// </param>
        public StrategyService(ILiveObjectDescriptor descriptor, ILiveObjectContainer container)
            : base(descriptor, container)
        {
            this.TrackingCells = new Dictionary<Guid, IStrategyPluginTrackingCell>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// The trackingCells.
        /// </summary>
        public IDictionary<Guid, IStrategyPluginTrackingCell> TrackingCells { get; private set; }

        #endregion

        #region Implemented Interfaces

        #region ILiveObject

        /// <summary>
        /// The on activation context.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="activationEventArgs">
        /// The activation context.
        /// </param>
        public override void OnActivation(object sender, ActivationEventArgs activationEventArgs)
        {
            // Some plugin was created
            if (activationEventArgs is ActivationEventArgs.OwnedPluginCreateDestroy)
            {
                var plugin = (activationEventArgs as ActivationEventArgs.OwnedPluginCreateDestroy).Plugin as IStrategyPlugin;
                switch (activationEventArgs.Type)
                {
                    case EActivationType.Activation:

                        // Create the tracking cell
                        this.TrackingCells[plugin.Descriptor.ID] = new StrategyPluginTrackingCell(this, plugin);
                        break;

                    case EActivationType.Desactivation:

                        // Remove tracking cell
                        this.TrackingCells.Remove(plugin.Descriptor.ID);
                        break;
                }
            }
        }

        #endregion

        #endregion
    }
}